package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import diploma.kisel.modelas.service.UserServiceImpl;
import diploma.kisel.modelas.service.api.UserService;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.gui.common.CurrentVar;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * It is the first window. It is designed for authorization users.
 * 
 * @author Nikolay Kisel
 *
 */
public class AuthorizationWindow extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;

	private UserService userService = new UserServiceImpl();

	private static AuthorizationWindow frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new AuthorizationWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AuthorizationWindow() {
		setTitle("Авторизация");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 437, 164);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JLabel label = new JLabel("Логин:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 20,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10,
				SpringLayout.WEST, contentPane);
		contentPane.add(label);

		textField = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.NORTH, textField, -3,
				SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.EAST, textField, -15, SpringLayout.EAST, contentPane);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel label_1 = new JLabel("Пароль:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label_1, 23, SpringLayout.SOUTH, label);
		sl_contentPane.putConstraint(SpringLayout.WEST, label_1, 10, SpringLayout.WEST, contentPane);
		contentPane.add(label_1);

		passwordField = new JPasswordField();
		sl_contentPane.putConstraint(SpringLayout.WEST, textField, 0, SpringLayout.WEST, passwordField);
		sl_contentPane.putConstraint(SpringLayout.NORTH, passwordField, -3,
				SpringLayout.NORTH, label_1);
		sl_contentPane.putConstraint(SpringLayout.WEST, passwordField, 63, SpringLayout.EAST, label_1);
		sl_contentPane.putConstraint(SpringLayout.EAST, passwordField, -15, SpringLayout.EAST, contentPane);
		passwordField.setColumns(10);
		contentPane.add(passwordField);

		JButton button = new JButton("Зарегистрироваться");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openRegistrationWindow();
			}
		});
		contentPane.add(button);

		JButton button_1 = new JButton("Войти");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, button_1, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, button_1, -15, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, button_1);
		sl_contentPane.putConstraint(SpringLayout.EAST, button, -5, SpringLayout.WEST, button_1);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				checkPassword();
			}
		});
		contentPane.add(button_1);
		contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(
				new Component[] { textField, passwordField, button_1, label,
						label_1, button }));
	}

	public void openRegistrationWindow() {
		RegistrationWindow registrationWindow = new RegistrationWindow();
		RegistrationWindow.setFrame(registrationWindow);
		registrationWindow.setVisible(true);
	}

	public void checkPassword() {
		String login = textField.getText();
		String password = passwordField.getText();
		if (userService.checkPassword(login, password)) {
			MainWindow mainWindow = new MainWindow();
			MainWindow.setFrame(mainWindow);
			mainWindow.setVisible(true);
			CurrentVar.setCurrentUser(userService.findById(login));
			frame.setVisible(false);
		} else
			JOptionPane.showMessageDialog(frame,
					"Неправильный логин или пароль.", "Сообщение", JOptionPane.INFORMATION_MESSAGE);

	}


}
